jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("surveyquery");  //Set current page
	App.init(); //Initialise plugins and elements
	
	$('#qst').change(function(){
		var date =new Date($('#qst').val());
	});
	
	//列表查询事件
	$('#search').click(function(){
		$('#questionTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	//新增问卷
	$('#addQuestion').click(function(){
		setModalSize(0.7, 0.7);
		
		$('#ifModal').attr("src", "add2.html");
		
		$('#modalTitle').html("新增问卷");
		$("#modalMenu").modal('show');
	});
	
	//清空表单数据事件
	$("#modalMenu").on("hidden.bs.modal", function() {
		$("#questionnaireTitle").val("");
		$("#qst").val("");
		$("#qen").val("");
		$("#disc").val("");
	});
	
	// 保存数据,下一步事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
});

function initTable(){	
	$('#questionTable').bootstrapTable({
		method: 'post',
	    url: projectPath + 'question/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	QUESTIONNAIRETITLE: $('#title').val(),
                	ST:$('#st').val(),
                	EN:$('#en').val()
                };
        },
	    columns: [{  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
                var page = $('#questionTable').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }         
	    }, {
	        field: 'QUESTIONNAIRETITLE',
	        title: '问卷标题'
	    }, {
	        field: 'QUESTIONNAIREID',
	        visible : false	        
	    }, {	    	
	        field: 'EFFECTIVESTARTDATE',
	        title: '有效起始日期'
	    }, {
	        field: 'EFFECTIVEENDDATE',
	        title: '有效结束日期'
	    },{
	        field: 'QUESTIONNAIREDISC',
	        title: '问卷说明',
	        visible: false
	    },{
            field: 'STATUS',
            title: '状态',
            events :events,
            formatter : function(value, row, index) {
            	if(value=="0"){
            		return '<span style="color:#ed5565" >未发布</span>';
            	}else {
            		return '<span>已发布</span>';
            	}
            }
        },{
            field: 'operate',
            title: '问卷结果',
            align: 'center',
            width: '100px',
            events: events,
            formatter: function(value, row, index){
            	if(row.STATUS==1){
	            	return [
	            		'<button type="button" class="Result btn btn-inverse  btn-sm"><span class="glyphicon glyphicon-signal"></span>问卷结果</button>'
	        		].join('');
            	}else{
	            	return [
	            		'<button disabled="disabled" type="button" class="result btn btn-inverse  btn-sm"><span class="glyphicon glyphicon-signal"></span>问卷结果</button>'
	            	].join('');;
            	}
            }
        },{
            field: 'operate1',
            title: '操作',
            align: 'center',
            width: '160px',
            events: events,
            formatter: function(value, row, index){
            	if (row.STATUS == 0) {
            		return [
            			'<button type="button" class="Edit btn btn-info  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
            			'<button type="button" class="Delete btn btn-danger  btn-sm"><span class="glyphicon glyphicon-minus"></span>删除</button>'
            		].join('');  
            	} else {
            		return [
            			'<button type="button" class="Detail btn btn-info  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-search"></span>预览</button>',
            			'<button type="button" class="Delete btn btn-danger  btn-sm"><span class="glyphicon glyphicon-minus"></span>删除</button>'
            		].join(''); 
            	}
            }
        }]
	});
}
window.events={
		'click .Edit' : function(e, value, row, index) {
			setModalSize(0.7, 0.7);
			
			$('#ifModal').attr("src", "edit.html?pk="+ row.QUESTIONNAIREID + "&operator=edit");
			
			$('#modalTitle').html("问卷修改");
			$("#modalMenu").modal('show');
		},
		'click .Detail' : function(e, value, row, index) {
			var documentW = $(window).width();
			var documentH = $(window).height();
			$('#modalW3').width(documentW * 0.7);
			$('#modalH3').height(documentH * 0.7);
			$('#ifModal3').attr("src", "preview.html?pk="+row.QUESTIONNAIREID);
			$('#modalTitle3').html("问卷预览");
			$("#modalMenu3").modal('show');
		},
		'click .Result ':function(e, value, row, index){
			var documentW = $(window).width();
			var documentH = $(window).height();
			$('#modalW3').width(documentW * 0.7);
			$('#modalH3').height(documentH * 0.7);
			$('#ifModal3').attr("src", "result.html?pk="+row.QUESTIONNAIREID);
			$('#modalTitle3').html("问卷统计");
			$("#modalMenu3").modal('show');
		},
		'click .Delete' :function(e, value, row, index) {
			window.wxc.xcConfirm('是否<strong>删除</strong>问卷数据?<br /><span style="color:red;">['+ row.QUESTIONNAIRETITLE +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
				onOk:function(v){
					$.ajax({ 
    					url: projectPath + 'question/delete.do',
    					data: {"QUESTIONNAIREID": row.QUESTIONNAIREID},
    					dataType: "json",
    					success: function(data){	            						
    						if(data.result == 1){
    				        	$('#questionTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
    						}
    			      	}
    				});
				}
			});
		}
}

function setModalSize(widthPercent, heightPercent){
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * widthPercent);
	$('#modalH').height(documentH * heightPercent);
}

function closeModal(id){
	$("#" + id).modal('hide');
	$('#questionTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
}